package com.yuyang.gulimall.seckill.interceptor;

import com.yuyang.common.constant.AuthServerConstants;
import com.yuyang.common.vo.MemberResponseVo;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author yu yang
 * @date 2023/2/4 -15:40
 */
@Component
public class LoginUserInterceptor implements HandlerInterceptor {
    public static ThreadLocal<MemberResponseVo> loginUser = new ThreadLocal<>();

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        AntPathMatcher antPathMatcher = new AntPathMatcher();
        boolean match = antPathMatcher.match("/kill/**", request.getRequestURI());
        if (match){
            HttpSession session = request.getSession();
            MemberResponseVo memberResponseVo = (MemberResponseVo) session.getAttribute(AuthServerConstants.LOGIN_USER);
            if (memberResponseVo == null){
                session.setAttribute("msg","请先登录");
                response.sendRedirect("http://auth.gulimall.com/login.html");
                return false;
            }
            loginUser.set(memberResponseVo);
            return true;
        }

        return true;
    }
}
